package cn.lsh.service.impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.lsh.mapper.UserMapper;
import cn.lsh.mapper.UserRoleMapper;
import cn.lsh.pojo.User;
import cn.lsh.service.UserService;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;

	//根据用户名查找用户
	@Override
	public User findUserByUserName(String username) {
		// TODO Auto-generated method stub
		return userMapper.findByUserName(username);
	}

	//创建用户
	@Override
	public User createUser(User user) {
		// TODO Auto-generated method stub
		return userMapper.createUser(user);
	}

	//修改密码
	@Override
	public void changePassword(Long userId, String newPassword) {
		// TODO Auto-generated method stub
		userMapper.updatePassword(userId, newPassword);
	}

	//添加用户-角色关系
	@Override
	public void correlationRoles(Long userId, Long... roleIds) {
		// TODO Auto-generated method stub
		if(roleIds.length>0){
			for(Long roleId:roleIds){
				userRoleMapper.insertUserRole(userId, roleId);
			}
		}
	}

	//移除用户-角色关系
	@Override
	public void uncorrelationRoles(Long userId, Long... roleIds) {
		// TODO Auto-generated method stub
		if(roleIds.length>0){
			for(Long roleId:roleIds){
				userRoleMapper.deleteUserRole(userId, roleId);
			}
		}
	}

	//根据用户名查找其角色
	@Override
	public Set<String> findRoles(String username) {
		// TODO Auto-generated method stub
		return userMapper.findRoles(username);
	}

	//根据用户名查找其权限
	@Override
	public Set<String> findPermissions(String username) {
		// TODO Auto-generated method stub
		return userMapper.findPermissions(username);
	}

	@Override
	public List<User> findUsers() {
		// TODO Auto-generated method stub
		return userMapper.findUsers();
	}
	
	
}
